<?php
/**
 * Created by PhpStorm.
 * User: fenfen
 * Date: 2016/12/27
 * Time: 11:03
 */

namespace bc\model;

use bc\ar\MActiveRecord;
class InputModel  extends MActiveRecord
{

    /**
     *
     */
    public static function tableName()
    {
        return '{{%input}}';
    }

    /*
     * 根据页面id获取页面属性
     * */
    public function getInputFromPage($page_id)
    {
        if(empty($page_id)){
            return array();
        }

        $params=array(
            'a.belongs_page' => 1,
            'a.pid' => 0
        );
        $this->columns='a.id as aid,a.pid as apid,a.input_des as ainput_des,a.input_name as ainput_name,a.input_id as ainput_id,a.input_value as ainput_value,a.input_html as ainput_html,a.input_type_id as ainput_type_id,b.id as bid,b.pid as bpid,b.input_des as binput_des,b.input_name as binput_name,b.input_id as binput_id,b.input_value as binput_value,b.input_html as binput_html,b.input_type_id as binput_type_id,c.id as cid,c.pid as cpid,c.input_des as cinput_des,c.input_name as cinput_name,c.input_id as cinput_id,c.input_value as cinput_value,c.input_html as cinput_html,c.input_type_id as cinput_type_id';
        $row = $this->find()->select($this->columns)->from(self::getTableSchema()->name . ' as a')->leftJoin('bc_input b', 'a.id = b.pid')->leftJoin('bc_input c', 'b.id = c.pid')->where($params)->orderBy ( 'a.sort,b.sort,c.sort' . ' DESC' );
        if($row)
            return $row->asArray()->all();

        return null;
    }

    /*
     * 配置数据结构
     * */
    public function datahandling($data)
    {
//        var_dump($data);
//        if(empty($data))
//        {
//            return json_encode(array());
//        }
//        var_dump($data);
        $return =array();

        foreach($data as $k => $v)
        {

            $return[$v['aid']]['id']=$v['aid'];
            $return[$v['aid']]['pid']=$v['apid'];
            $return[$v['aid']]['input_des']=$v['ainput_des'];
            $return[$v['aid']]['input_name']=$v['ainput_name'];
            $return[$v['aid']]['input_id']=$v['ainput_id'];
            $return[$v['aid']]['input_value']=$v['ainput_value'];
            $return[$v['aid']]['input_html']=$v['ainput_html'];
            $return[$v['aid']]['input_type_id']=$v['ainput_type_id'];

            //获取二级信息
            if(!empty($v['bid'])){
                $return[$v['aid']]['bid'][$v['bid']]['id']=$v['bid'];
                $return[$v['aid']]['bid'][$v['bid']]['pid']=$v['bpid'];
                $return[$v['aid']]['bid'][$v['bid']]['input_des']=$v['binput_des'];
                $return[$v['aid']]['bid'][$v['bid']]['input_name']=$v['binput_name'];
                $return[$v['aid']]['bid'][$v['bid']]['input_id']=$v['binput_id'];
                $return[$v['aid']]['bid'][$v['bid']]['input_value']=$v['binput_value'];
                $return[$v['aid']]['bid'][$v['bid']]['input_html']=$v['binput_html'];
                $return[$v['aid']]['bid'][$v['bid']]['input_type_id']=$v['binput_type_id'];

                //获取三级信息
                if(!empty($v['cid'])){
                    $return[$v['aid']]['bid'][$v['bid']]['cid'][$v['cid']]['id']=$v['cid'];
                    $return[$v['aid']]['bid'][$v['bid']]['cid'][$v['cid']]['pid']=$v['cpid'];
                    $return[$v['aid']]['bid'][$v['bid']]['cid'][$v['cid']]['input_des']=$v['cinput_des'];
                    $return[$v['aid']]['bid'][$v['bid']]['cid'][$v['cid']]['input_name']=$v['cinput_name'];
                    $return[$v['aid']]['bid'][$v['bid']]['cid'][$v['cid']]['input_id']=$v['cinput_id'];
                    $return[$v['aid']]['bid'][$v['bid']]['cid'][$v['cid']]['input_value']=$v['cinput_value'];
                    $return[$v['aid']]['bid'][$v['bid']]['cid'][$v['cid']]['input_html']=$v['cinput_html'];
                    $return[$v['aid']]['bid'][$v['bid']]['cid'][$v['cid']]['input_type_id']=$v['cinput_type_id'];
                }
            }
        }
        return json_encode($return);
    }
}